package com.hp.tiger.chapter10;

import java.util.HashMap;
import java.util.Map;

/**
 * @author XavierDong
 **/

/**
 * 始终以当前线程 作为 key值
 *
 * @param <T>
 */
public class ThreadLocalSimulator<T> {

    private final Map<Thread, T> storage = new HashMap<>();

    public void set(T t) {
        synchronized (this) {
            Thread key = Thread.currentThread();
            storage.put(key, t);
        }
    }


    public T get() {
        synchronized (this) {
            Thread key = Thread.currentThread();
            return storage.get(key) == null ? initiaValue() : storage.get(key);
        }
    }

    public T initiaValue() {
        return null;
    }

}
